import Axios from 'axios'
import qs from 'qs'
import { showLoading, hideLoading } from '@/modules/loading';

export default function filter() {
    Axios.interceptors.request.use(function (config) {
        // 请求时携带token,如果有的话
        if (localStorage.hasOwnProperty("higame-token")) {
            config.headers['Authorization'] = 'Bearer ' + localStorage.getItem("higame-token");
        }
        if (config.method === 'post') {
            if (config.data == null) {
                config.data = {}
            }
            config.data = qs.stringify(config.data)
        }
        showLoading();
        return config
    }, function (error) {
        return Promise.reject(error)
    })
    // respone拦截器
    Axios.interceptors.response.use(res => {
        if (res.data.code != 200) {
            return Promise.reject(res.data)
        } else {
            hideLoading();
            return res.data
        }

    }, error => {
        hideLoading();
        return Promise.reject(error.response.data)
    })

}